package com.mz.mattress.client.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.api.entity.MemberCard;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface MemberCardMapper extends BaseMapper<MemberCard> {

    Page<MemberCard> listPage(Page page,
                              @Param("memberId") Long memberId,
                              @Param("status") Integer status);

    @Select("select * from mz_member_card where order_id=#{orderId}")
    List<MemberCard> selectByOrderId(@Param("orderId") Long orderId);

    //是否已发放过
    @Select("select count(*) from mz_member_card where member_id=#{memberId} and card_id=#{cardId}")
    Integer selectCountByMemberId(@Param("memberId") Long memberId, @Param("cardId") Integer cardId);

    //这个月是否已发放过
    @Select("select count(*) from mz_member_card where member_id=#{memberId} and card_id=#{cardId} and create_time>#{month}")
    Integer selectCountMonthByMemberId(@Param("memberId") Long memberId, @Param("cardId") Integer cardId, @Param("month") String month);

    //标记已过期 找已过期，状态是未使用卡项,
    @Update("update mz_member_card set status=2 where status=0 and end_time<now()")
    void updateCardExpire();

}
